<?php namespace Phpcmf\Model\Quan; 

// 付款模型示例
class Buympsan extends \Phpcmf\Model
{
	public $mytable; // 模块表名称
	public $module; // 模块属性

    private $row;
	// 初始化模型
	public function init($table) {
	    $this->mytable = dr_module_table_prefix($table, SITE_ID);
	    return $this;
	}

    // 付款表单格式
    public function get_myfield() {

        return [
            'fieldtype' => 'Pay', // 字段类型
            'fieldname' => 'mpsanyuedujinbi',
            'setting' => [
                'option' => [
                    'payfile' => 'buympsan.html', // 模板文件
                    'is_finecms' => 1, // 是否启用余额付款
                ],
            ]
        ];
    }

    // 付款类型名称
    public function paytype() {
        return  '<span class="label label-danger"> 圈子 </span>'; // 最好2个汉字表述
    }

    // 付款前的权限验证，返回null表示可进行付款，返回字符串是就输出字符串
    // $id 记录id; $paylog 支付表记录数组; $num 数量; $sku 自定义属性
    public function pay_before($id, $num, $sku, $siteid) {
        return '';
    }

    // 付款价格
    // $id 记录id; $num 数量; $sku 自定义属性; $siteid 站点id
    public function get_price($id, $num, $sku, $siteid) {

        // 计算付款价格
        $data = $this->_get_row($id, $siteid);

        return $data['mpsanyuedujinbi']; // 测试200
    }

    // 付款数据
    // $id 记录id; $num 数量; $sku 自定义属性; $siteid 站点id
    public function get_row($id, $num, $sku, $siteid) {

        // 查询数据记录，判断是否存在
        $data = $this->_get_row($id, $siteid);
		//dr_debug('shijianid', $data);
		if ($data['title']) {
			$datatitle = $data['title'];
		} else {
			$datatitle = dr_strcut(dr_clearhtml($data['neirong']), 70);
		}
        return [
            'price' => $this->get_price($id, $num, $sku, $siteid),
            'title' => '音频阅读《'.$datatitle.'》',
			'shourutitle' => '音频阅读收入《'.$datatitle.'》',
            'sell_uid' => $data['uid'], // 商家uid
            'sell_username' => $data['author'], // 商家账号
        ];
    }

    // 付款成功
    // $id 记录id; $paylog 支付表记录数组; $num 数量; $sku 自定义属性
    public function success($id, $paylog, $num, $sku) {

        // 支付成功之后的回调处理动作
        $data = $this->_get_row($id, $paylog['site']);

        // 打开给商家
		
		//
		$tianshus = \Phpcmf\Service::M()->db->table($paylog['site'].'_quan_form_son')->where('id', $id)->get()->getRowArray();
		$tianshu = (int)$tianshus['yxsj'];
		if ($tianshu == 0) {
			$tianshu = 30000;
		} else {
			$tianshu = $tianshu;
		}
		//dr_debug('$tianshu', $tianshu);
		//判断id更新
		$shijianid = \Phpcmf\Service::M()->db->table($paylog['site'].'_quan_mpsan_buy')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
		//dr_debug('shijianid', $shijianid['id']);
		//dr_debug('shijianid', $shijianid);
		//$shijianid
		if ($shijianid) {
			// 更新记录表->where('id', $row['id'])->update(['status' => 2])
			\Phpcmf\Service::M()->db->table($paylog['site'].'_quan_mpsan_buy')->where('uid', $this->uid)->where('cid', $id)->update([
				'inputtime' => SYS_TIME,
				'endtime' => SYS_TIME + $tianshu * 3600 * 24,
			]);
		} else {
			// 新增记录表
			dr_debug('value', $paylog['value']);
			\Phpcmf\Service::M()->db->table($paylog['site'].'_quan_mpsan_buy')->insert(array(
			    'cid' => $id,
			    'uid' => $paylog['uid'],
				'inputtime' => SYS_TIME,
				'jianglirmb' => $tianshus['mpsanyuedujinbi'],
				'endtime' => SYS_TIME + $tianshu * 3600 * 24,
			));
		}
        $bicheng = \Phpcmf\Service::M()->table($paylog['site'].'_quan_shezhi')->where('dirname', 'quan')->getRow();
		$bichengs = dr_string2array($bicheng['setting']);
		//$bicheng = $bicheng['setting']['pages']['quan_shoucheng'] * 100%;
		// 提成
		
		$bicheng = $bichengs['pages']['quan_shoucheng'];
		$shourubi = 1 - $bichengs['pages']['quan_shoucheng'];
		$shouru = $bichengs['pages']['quan_shouru'];
		//dr_debug('文件名称', $shouru);
		if ($bicheng == 0) {
			$feiyong = abs($paylog['value']);
			$shourubi = 0;
		} else {
			$feiyong = abs($paylog['value']) * $bicheng;
			$shourubi = abs($paylog['value']) * $shourubi;
		}
		$feiyong = abs($paylog['value']) * $bicheng;
        // 打款到收款人账户
        \Phpcmf\Service::M('pay')->add_money($paylog['touid'], $feiyong);
        // 收款人增加一条收入记录
        \Phpcmf\Service::M('pay')->add_paylog([
            'uid' => $paylog['touid'],
            'username' => $paylog['tousername'],
            'touid' => $paylog['uid'],
            'tousername' => $paylog['username'],
            'mid' => $paylog['mid'],
            'title' => $paylog['title'].'收入',
            'value' => $feiyong,
            'type' => $paylog['type'],
            'status' => 1,
            'result' => $paylog['id'],
            'paytime' => SYS_TIME,
            'inputtime' => $paylog['inputtime'],
        ]);
		// 总站收款人账户
		\Phpcmf\Service::M('pay')->add_money($shouru, $shourubi);
		// 总站收款人增加一条收入记录
		\Phpcmf\Service::M('pay')->add_paylog([
		    'uid' => $shouru,
		    'username' => $paylog['tousername'],
		    'touid' => $paylog['uid'],
		    'tousername' => $paylog['username'],
		    'mid' => $paylog['mid'],
		    'title' => $paylog['title'].'收入',
		    'value' => $shourubi,
		    'type' => $paylog['type'],
		    'status' => 1,
		    'result' => $paylog['id'],
		    'paytime' => SYS_TIME,
		    'inputtime' => $paylog['inputtime'],
		]);
		//判断id更新
		/*$jiaruid = \Phpcmf\Service::M()->db->table($paylog['site'].'_quan_cart')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
		if ($jiaruid) {
			
		} else {
			//加入圈子
			\Phpcmf\Service::M()->table($paylog['site'].'_quan_cart')->insert([
				'cid' => $id,
				'uid' => $paylog['uid'],
				'read_name' => '',
				'read_url' => '',
				'inputtime' => SYS_TIME,
			]);
		}*/
		//更新购买人数统计数量
		$jiarutongji = \Phpcmf\Service::M()->db->table($paylog['site'].'_quan_mpsan_buy')->where('uid', $this->uid)->where('cid', $id)->countAllResults();
		// 更新记录表
		\Phpcmf\Service::M()->db->table($paylog['site'].'_quan_form_son')->where('id', $id)->update([
			'jiugetushuliang' => $jiarutongji,
		]);
    }

    // 是否购买了
    public function is_buy($uid, $cid) {
		//dr_debug('$uid', $uid);
        return \Phpcmf\Service::M()->table(SITE_ID.'_quan_mpsan_buy')->where('uid', $uid)->where('cid', $cid)->getRow();
    }

    // 根据id查询表数据
    // $id 记录id;  $siteid 站点id
    private function _get_row($id, $siteid) {

        if (isset($this->row[$id]) && $this->row[$id]) {
            return $this->row[$id];
        }
		
        $this->row[$id] = $this->table($siteid.'_quan_form_son')->get($id);

        return $this->row[$id];
    }


    // 付款成功跳转URL
    // $id 记录id; $paylog 支付表记录数组
    public function call_url($id, $paylog) {
        $data = $this->_get_row($id, $paylog['site']);
		$url = "index.php?s=quan&c=son&id=".$data['id']; 
        return dr_url_prefix($url); // 跳转
    }
}